#include "OLButton.h"
#include "OLPixmapWidget.h"
#include "OLAppTemplateDefs.h"

#include <QtGui/QMouseEvent>
#include <QtGui/QPaintEvent>
#include <QtGui/QPainter>
#include <QtGui/QHBoxLayout>
#include <QtGui/QLabel>
#include <QtGui/QLayout>

OLButton::OLButton(QWidget *parent) :
    QWidget(parent)
{
    m_flags=0;
    m_text=0;
    m_iconContainer = 0;
    setMinimumHeight(FINGER_SIZE-10);
}

OLButton::OLButton(const QString& text, QWidget *parent)
    : QWidget(parent)
{
    m_flags=0;
    m_text=0;
    m_iconContainer = 0;
    setMinimumHeight(FINGER_SIZE-10);

    textButton(text);
}

QHBoxLayout* OLButton::baseContruct()
{
    delete layout();
    delete m_text;
    m_text=0;
    delete m_iconContainer;
    m_iconContainer = 0;

    QHBoxLayout* layout = new QHBoxLayout(this);
    layout->setContentsMargins(5,5,5,5);
    layout->setSpacing(5);
    return layout;
}

void OLButton::textButton(const QString& text, Qt::Alignment alignment)
{
    QHBoxLayout* layout = baseContruct();

    m_text = new QLabel(text, this);
    QPalette btnPalette = m_text->palette();
    btnPalette.setColor(QPalette::WindowText, OLStyles::textColor());
    m_text->setPalette(btnPalette);
    layout->addWidget(m_text, 0, alignment);
}

void OLButton::iconButton(ButtonIcons icons, Qt::Alignment alignment)
{
    m_icons = icons;
    QHBoxLayout* layout = baseContruct();

    m_iconContainer = new QLabel(this);
    layout->addWidget(m_iconContainer, 0, alignment);
    m_iconContainer->setPixmap(m_icons.first);
}

void OLButton::textIconButton(const QString& text, ButtonIcons icons, Qt::Alignment alignment)
{
    m_icons = icons;
    QHBoxLayout* layout = baseContruct();

    if(alignment==Qt::AlignRight || alignment==Qt::AlignCenter)
        layout->addStretch();

    m_text = new QLabel(text, this);
    QPalette btnPalette = m_text->palette();
    btnPalette.setColor(QPalette::WindowText, OLStyles::textColor());
    m_text->setPalette(btnPalette);
    layout->addWidget(m_text);

    if(alignment==Qt::AlignCenter)
        layout->addStretch();

    m_iconContainer = new QLabel(this);
    layout->addWidget(m_iconContainer);
    m_iconContainer->setPixmap(m_icons.first);

    if(alignment==Qt::AlignLeft)
        layout->addStretch();
}

void OLButton::iconTextButton(ButtonIcons icons, const QString& text, Qt::Alignment alignment)
{
    m_icons = icons;
    QHBoxLayout* layout = baseContruct();

    if(alignment==Qt::AlignRight)
        layout->addStretch();

    m_iconContainer = new QLabel(this);
    layout->addWidget(m_iconContainer);
    m_iconContainer->setPixmap(m_icons.first);

    if(alignment==Qt::AlignCenter)
        layout->addStretch();

    m_text = new QLabel(text, this);
    QPalette btnPalette = m_text->palette();
    btnPalette.setColor(QPalette::WindowText, OLStyles::textColor());
    m_text->setPalette(btnPalette);
    layout->addWidget(m_text);

    if(alignment==Qt::AlignLeft)
        layout->addStretch();
}

void OLButton::hideIcon()
{
    if(m_iconContainer)
        m_iconContainer->hide();
}

void OLButton::showIcon()
{
    if(m_iconContainer)
        m_iconContainer->show();
}

void OLButton::hideText()
{
    if(m_text)
        m_text->hide();
}

void OLButton::showText()
{
    if(m_text)
        m_text->show();
}

void OLButton::setText(const QString& text)
{
    if(m_text)
        m_text->setText(text);
}

void OLButton::setTextStyleSheet(const QString& style)
{
    if(m_text)
        m_text->setStyleSheet(style);
}


void OLButton::setIcon(const QPixmap& icon_up, const QPixmap& icon_down)
{
    if(m_iconContainer) {
        m_icons.first = icon_up;
        m_icons.second = icon_down;
    }
}

void OLButton::paintEvent(QPaintEvent* e)
{
    QPainter p(this);
    p.setRenderHint(QPainter::Antialiasing);
    p.setPen(Qt::NoPen);

    // draw the click feedback if button pressed

    if( ((m_flags & pressed) || (m_flags & highlighted)) && !(m_flags & disabled)) {
        if(m_flags & background) {
            // draw pressed button with a background
            p.setBrush(OLStyles::buttonBackgroundHighlightBrush(rect()));
            p.setPen(OLStyles::buttonHighlightBorder());
            //p.drawRoundedRect(rect(),5,5);
            p.drawRect(rect());
        } else {
            // draw pressed button without a background
            p.setPen(OLStyles::buttonHighlightBorder());
            p.setBrush(OLStyles::buttonHighlightBrush(rect()));
            p.drawRect(rect());
        }
    } else if(m_flags & background) {
        // draw background
        p.setBrush(OLStyles::buttonBackgroundBrush(rect()));
        p.setPen(OLStyles::buttonBorder());
        //p.drawRoundedRect(rect(),5,5);
        p.drawRect(rect());
    }

}

void OLButton::mousePressEvent(QMouseEvent* e)
{
    if (e->button() != Qt::LeftButton)
        return;

    if(m_text) {
        QPalette btnPalette = m_text->palette();
        btnPalette.setColor(QPalette::WindowText, OLStyles::textHighlightColor());
        m_text->setPalette(btnPalette);
    }

    if(m_iconContainer && !m_icons.second.isNull()) {
        m_iconContainer->setPixmap(m_icons.second);
    }

    m_flags |= pressed;
    emit pressedChanged(m_flags & pressed);
    update();
}

void OLButton::mouseReleaseEvent(QMouseEvent* e)
{
    if( m_flags & pressed && !(m_flags & disabled) )
        emit clicked();

    if(m_text) {
        QPalette btnPalette = m_text->palette();
        btnPalette.setColor(QPalette::WindowText, OLStyles::textColor());
        m_text->setPalette(btnPalette);
    }

    if(m_iconContainer && !m_icons.second.isNull()) {
        m_iconContainer->setPixmap(m_icons.first);
    }

    m_flags &= ~pressed;
    emit pressedChanged(m_flags & pressed);
    update();
}

void OLButton::mouseMoveEvent(QMouseEvent* e)
{
    bool oldValue = m_flags & pressed;
    if( rect().contains(e->pos()) ) {
        m_flags |= pressed;
    } else {
        m_flags &= ~pressed;
    }

    if( (m_flags & pressed)!=oldValue ) {
        if(m_flags & pressed) {
            if(m_text) {
                QPalette btnPalette = m_text->palette();
                btnPalette.setColor(QPalette::WindowText, OLStyles::textHighlightColor());
                m_text->setPalette(btnPalette);
            }

            if(m_iconContainer && !m_icons.second.isNull()) {
                m_iconContainer->setPixmap(m_icons.second);
            }
        } else {
            if(m_text) {
                QPalette btnPalette = m_text->palette();
                btnPalette.setColor(QPalette::WindowText, OLStyles::textColor());
                m_text->setPalette(btnPalette);
            }

            if(m_iconContainer && !m_icons.second.isNull()) {
                m_iconContainer->setPixmap(m_icons.first);
            }
        }

        emit pressedChanged(m_flags & pressed);
        update();
    }
}

void OLButton::disable()
{
    m_flags |= disabled;
}

void OLButton::enable()
{
    m_flags &= ~disabled;
}

void OLButton::highlight()
{
    m_flags |= highlighted;
    update();
}

void OLButton::unhighlight()
{
    m_flags &= ~highlighted;
    update();
}

void OLButton::toggleHighlight()
{
    m_flags ^= highlighted;
    update();
}

void OLButton::transparent()
{
    m_flags &= ~background;
    update();
}

void OLButton::opaque()
{
    m_flags |= background;
    update();
}

